package com.rh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rh.entity.UcenterMember;
import com.rh.entity.vo.UcenterMemberVo;
import com.rh.entity.vo.UcenterRegisterVo;
import com.rh.exception.EducationException;
import com.rh.mapper.UcenterMemberMapper;
import com.rh.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rh.utils.JwtUtils;
import com.rh.utils.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * 会员表 服务实现类
 * @author rh
 * @since 2021-10-19
 */
@Service
@Transactional
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 登陆
     */
    @Override
    public String login(UcenterMember ucenterMember) {
        //先获取邮箱和密码
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new EducationException(20001,"登陆失败,用户名或密码为空");
        }
        //判断邮箱
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember user = baseMapper.selectOne(wrapper);
        //对象判空
        if(user==null){
            throw new EducationException(20001,"登陆失败,该用户不存在,请先注册");
        }
        //判断密码
        //输入的密码加密和用户密码进行比较
        if (!MD5.encrypt(password).equals(user.getPassword())){
            throw new EducationException(20001,"登陆失败,用户名或密码错误");
        }
        //判断用户是否被禁用
        if(user.getIsDisabled()){
            throw new EducationException(20001,"登陆失败,该用户被禁用");
        }
        //生成token
        String token = JwtUtils.createJwtToken(user.getId(), user.getNickname());
        return token;
    }

    /**
     * 注册
     * @param ucenterRegisterVo
     * @return
     */
    @Override
    public void register(UcenterRegisterVo ucenterRegisterVo) {

        String mobile = ucenterRegisterVo.getMobile();
        String password = ucenterRegisterVo.getPassword();
        String nickname = ucenterRegisterVo.getNickname();
        String code = ucenterRegisterVo.getCode();

        //非空判断
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)||StringUtils.isEmpty(nickname)||StringUtils.isEmpty(code)){
            throw new EducationException(20001,"注册失败,请返回重新注册");
        }
        //判断验证码是否正确
        //获取redis中的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw new EducationException(20001,"注册失败,验证码不正确");
        }
        //判断手机号是否相同,如果数据库已经有一个手机号，不能存在相同手机号
        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("mobile",mobile);
        //使用mybatisplus中的selectCount方法,如果有值就会返回值的条数,这样好处是不用查过多的数据
        Integer count = baseMapper.selectCount(ucenterMemberQueryWrapper);
        if(count>0) {//count>0说明数据库中已经存在手机号的数据了，就不能让他添加
            throw new EducationException(20001,"注册失败,该手机号已存在");
        }
        //把数据添加到数据库中
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        //密码要MD5加密
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setNickname(nickname);
        //是否禁用，默认否是false
        ucenterMember.setIsDisabled(false);
        //todo 先设置一个默认头像(上传头像功能后续优化)
        ucenterMember.setAvatar("https://ps.ssl.qhimg.com/t015f590bb00aa069f4.jpg");
        //注册成功添加到数据库
        int insert = baseMapper.insert(ucenterMember);
        if (insert<=0){
            throw new EducationException(20001,"注册失败,请联系管理员");
        }
    }

    @Override
    public UcenterMemberVo getMemberInfoById(String memberId) {
        UcenterMember ucenterMember = baseMapper.selectById(memberId);
        UcenterMemberVo ucenterMemberVo = new UcenterMemberVo();
        BeanUtils.copyProperties(ucenterMember,ucenterMemberVo);
        return ucenterMemberVo;
    }

    /**
     * 根据openId获取信息
     * @param openid
     * @return
     */
    @Override
    public UcenterMember getMenberByOperid(String openid) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        return member;
    }

    /**
     * 根据日志查询注册人数  统计用
     * @param day
     * @return
     */
    @Override
    public Integer countRegisterDay(String day) {

        return  baseMapper.countRegisterDay(day);
    }

}
